﻿using RayDen.RayEngine.Core;
using RayDen.RayEngine.Core.Devices;
using RayDen.RayEngine.Core.Interface;
using RayDen.RayEngine.Core.Threads;
using RayDen.RayEngine.Managers;
using RayDen.RayEngine.Samplers;

namespace RayDen.RayEngine.Engines.ManyLights {
    public class MLEngine : BaseRenderingEngine
    {
        public const int MaxVpls = 160000;
        private MLEngineContext vpls;
        public MLEngine(RayEngineScene scene) : base(scene)
        {
            
            
        }

        public override bool PreprocessRequired
        {
            get { return true; }
        }

        public override void Preprocess(RayEngineScene scene, IntersectionEngine ie)
        {
            vpls.BuildMap(ie, scene);
        }

        public override RenderThread CreateRenderThread(int threadIndex, IntersectionEngine intersectionDevice, RayEngineScene scene, IImageFilm film, IFrameManager frameManager, SamplingContext samplingContext)
        {
            if (this.vpls == null)
            {
                vpls = new MLEngineContext() { surfaceSampler = (SurfaceSampler) samplingContext.SurfaceSampler };
            }
            return new DeviceRenderThread(threadIndex, intersectionDevice, scene, this.LowLatencySampling, film, samplingContext, vpls, frameManager);
        }

        public override ISampler CreateSampler(int width, int height, int maxPathDepth, int threadIndex, int screenStartLine, int windowHeight) {
            return SamplerFactory.Instance.CreateSampler(width,
                                                       height,
                                                       maxPathDepth,
                                                       screenStartLine,
                                                       LowLatencySampling,
                                                       threadIndex, windowHeight);
            /*
            var sampler = new UniformSampler { lowLatency = LowLatencySampling, tIdx = threadIndex, screenStartLine = screenStartLine > 0 ? screenStartLine + 1 : screenStartLine, screenWidth = width, screenHeight = height };
            return sampler;
             * */
        }

        public override Core.Interface.IImageFilm CreateFilm(int width, int height) {
            return new ImageFilm(width, height);
        }
    }
}
